package com.fc.advbox.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.fc.advbox.common.ResponseJson;
import com.fc.advbox.controller.admin.BaseController;
import com.fc.advbox.dto.investor.InvestorAcctDto;
import com.fc.advbox.dto.org.ContractRequest;
import com.fc.advbox.entity.AdvUserEntity;
import com.fc.advbox.entity.InvestorAcctEntity;
import com.fc.advbox.service.IInvestorAcctService;
import com.fc.advbox.service.IUserService;
import com.fc.advbox.utils.PageUtils;
import com.fc.advbox.utils.Query;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import javax.xml.ws.Response;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * InverstorController
 *
 * @author Gary.Jiang
 * @date 2018/6/26.15:11
 */

@RestController
@RequestMapping("/api/investor")
@Api(value="投资人相关的API", tags={"客户接口"})
public class InvestorController extends BaseController {

    @Autowired
    private IInvestorAcctService investorAcctService;

    /**
     * 查询客户列表
     */
    @PostMapping("/query")
    @ApiOperation(value = "查询客户列表",  httpMethod="POST",
            notes = " REST 方式请求," +
                    "Request Body: <ul>" +
                    "<li><i>可选参数: realName: string </i></li>" +
                    "<li><i>可选参数: type: string </i></li>" +
                    "<li><i>可选参数: status: string </i></li>" +
                    "<li><i>可选参数: userId: long </i></li>" +
                    "<li><i>可选参数: province: string </i></li>" +
                    "<li><i>默认参数: page: 1, 当前页，默认值1 </i></li>" +
                    "<li><i>默认参数: limit: 20 </i></li>" +
                    "</ul>")
    @RequiresPermissions("api:user:profit:view")
    public ResponseJson query(@RequestBody JSONObject params){
        //校对并赋值 offset和limit
        calculatePagination(params);

        Query query = new Query(params);
        List<InvestorAcctEntity> list = investorAcctService.queryList(query);
        int total = investorAcctService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
        return ResponseJson.success(investorAcctService.queryList(params));
    }

    @PostMapping("/queryTotalProfit")
    @ApiOperation(value="所有客户总收益", httpMethod = "POST",
            notes = " REST 方式请求," +
                    "Request Params: <ul>" +
                    "<li><i>可选参数: realName: string </i></li>" +
                    "<li><i>可选参数: type: string </i></li>" +
                    "<li><i>可选参数: userId: long </i></li>" +
                    "<li><i>可选参数: province: string </i></li>" +
                    "<li><i>默认参数: createTime: date, 格式：yyyy-MM-dd HH:mm:SS 例如 2018-06-19 07:54:01 </i></li>" +
                    "</ul>")
    @RequiresPermissions("api:user:profit:view")
    public ResponseJson<BigDecimal> queryTotalProfit(@RequestParam(required = false) Map<String, Object> params){
        BigDecimal totalProfit = investorAcctService.queryTotalProfit(params);
        return ResponseJson.success(totalProfit);
    }

    @ApiOperation(value="获取指定客户的账户列表", httpMethod = "GET",
            notes = " REST 方式请求," +
                    "Request Params: <ul>" +
                    "<li><i>参数: userId: string </i></li>" +
                    "</ul>")
    @GetMapping("/user/{userId}/acct")
    @RequiresPermissions("api:user:profit:view")
    public ResponseJson queryUserAccts(@PathParam("userId") @PathVariable("userId") Long userId){
        List<InvestorAcctEntity> acctInfo = investorAcctService.queryInvestorAcctByUserId(userId);
        if(acctInfo == null){
            return ResponseJson.error(ResponseJson.NOT_FOUND, "账户不存在");
        }
        return ResponseJson.success(acctInfo);
    }

    @ApiOperation(value="获取指定客户指定的账户信息", httpMethod = "GET",
            notes = " REST 方式请求," +
                    "Request Params: <ul>" +
                    "<li><i>参数: userId: string </i></li>" +
                    "<li><i>参数: acctId: string </i></li>" +
                    "</ul>")
    @GetMapping("/user/{userId}/acct/{acctId}")
    @RequiresPermissions("api:user:profit:view")
    public ResponseJson queryUserAcct(@PathParam("userId") @PathVariable("userId") Long userId,@PathParam("acctId") @PathVariable("acctId") Long acctId){
        InvestorAcctEntity acctInfo = investorAcctService.queryInvestorAcctByUserIdAndAcctId(userId,acctId);
        if(acctInfo == null){
            return ResponseJson.error(ResponseJson.NOT_FOUND, "账户不存在");
        }
        return ResponseJson.success(acctInfo);
    }


    @ApiOperation(value="获取某个客户的收益", httpMethod = "GET",
            notes = " REST 方式请求," +
                    "Request Params: <ul>" +
                    "<li><i>参数: userId: string </i></li>" +
                    "</ul>")
    @GetMapping("/profit/{userId}")
    @RequiresPermissions("api:user:profit:view")
    public ResponseJson queryUserProfit(@PathParam("userId") @PathVariable("userId") Long userId){
        InvestorAcctDto acctInfo = investorAcctService.queryInvestorProfitByUserId(userId);
        return ResponseJson.success(acctInfo);
    }
}
